package com.ttx.jsp.config;

import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.jasperreports.JasperReportsViewResolver;

/**
 * @author TimFruit
 * @date 20-3-5 下午4:38
 */
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter implements EnvironmentAware {
    Environment env;


    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        super.configureDefaultServletHandling(configurer);
        configurer.enable();
    }


    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
//        JasperReportsViewResolver vr=new JasperReportsViewResolver();

        InternalResourceViewResolver vr=new InternalResourceViewResolver();
        String prefix=env.getProperty("spring.mvc.view.prefix");
        vr.setPrefix(prefix);
        String suffix=env.getProperty("spring.mvc.view.suffix");
        vr.setSuffix(suffix);
        vr.setViewClass(JstlView.class);
        vr.setExposeContextBeansAsAttributes(true);

        registry.viewResolver(vr);
    }

    @Override
    public void setEnvironment(Environment environment) {
        env=environment;
    }
}
